<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Button Sequence Use Autocomplete Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  #subbtn1 { width: 40px; height: 30px; }
  #subbtn2 { width: 40px; height: 30px; }
  #area { padding: 10px; }
  #area input { display: block; }
  #subbtn { display: block; margin-top: 5px; }
  #area input, .ui-menu { width: 120px; }
  .bold { font-weight: bold; }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/ui_utils.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

var genProblem = function() {
  var task_state = -1.0;
  // click-button-sequence
  var button_state = -1.0
  var div = d3.select('#area');
  div.html(''); // clear previous problem, if any
  // var L = core.randi(0, 118); var U = core.randi(0, 118) + 50;
  var L = core.randi(0, 118); var U = core.randi(85, 118) + 50;
  div.append('button').attr('id', 'subbtn1').html('ONE');
  d3.select('#subbtn1').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn1').on('click', function(){
    if (button_state == -1.0) {
      button_state = 1.0
    } else {
      button_state = -1.0
    }
    console.log('click-button-sequence: ONE', button_state);
  });

  // var L = core.randi(0, 118); var U = core.randi(0, 118) + 50;
  var L = core.randi(0, 118); var U = core.randi(85, 118) + 50;
  div.append('button').attr('id', 'subbtn2').html('TWO');
  div.append('br');
  d3.select('#subbtn2').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn2').on('click', function(){
    if (button_state == 1.0) {
        task_state = 1.0
    } else {
        task_state = -1.0
    }
    console.log('click-button-sequence: TWO', task_state);
  });

  // use-autocomplete
  div.append('label').attr('for', 'tags').html('Tags: ');
  div.append('input').attr('id', 'tags');
  div.append('button').attr('id', 'subbtn').attr('class', 'secondary-action').html('Submit');

  ITEMS = ui_utils.COUNTRIES;
  d3.select('#tags')[0][0].value ='';

  var gtix = core.randi(0, ITEMS.length);
  // the below is complicated because we only want to match beginning of items in list
  $('#tags').autocomplete({
    source: function(request, response) {
      var matcher = new RegExp('^' + $.ui.autocomplete.escapeRegex(request.term), 'i');
      response($.grep(ITEMS, function(item){
          return matcher.test(item);
      }))
    }
  });

  var gtitem = ITEMS[gtix];

  var nf = core.randi(2, 5);
  var f = gtitem.slice(0,nf);

  var ne = core.randi(2, 5);
  var e = gtitem.slice(gtitem.length - ne);

  var match_end = core.randf(0,1) < 0.75;
  if(match_end) {
    // d3.select('#query').html('Click button ONE, then click button TWO, and then enter an item that starts with "<span class="bold">' + f + '</span>" and ends with "<span class="bold">' + e + '</span>".');
    d3.select('#query').html('Enter an item that starts with "<span class="bold">' + f + '</span>" and ends with "<span class="bold">' + e + '</span>", after clicking button ONE, then clicking button TWO.');
  } else {
    // d3.select('#query').html('Click button ONE, then click button TWO, and then enter an item that starts with "<span class="bold">' + f + '</span>".');
    d3.select('#query').html('Enter an item that starts with "<span class="bold">' + f + '</span>", after clicking button ONE, then clicking button TWO.');
  }

  d3.select('#subbtn').on('click', function(){
    var entered = d3.select('#tags')[0][0].value;
    var ff = entered.startsWith(f);
    var ee = entered.endsWith(e);
    var correct = ff & (ee || !match_end);
    var r = correct ? 1.0 : -1.0;
    console.log('use-autocomplete', r);
    if (task_state == 1.0) {
      core.endEpisode(r, r>0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <label for="tags">Tags: </label>
    <input id="tags">
    <button id="subbtn" class="secondary-action">Submit</button>
  </div>
</div>
</body>
</html>